package com.useeinfo.oa.open.admin.repair;

import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.CalculateUtil;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.EnterpriseBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.repair.biz.RepairOrderBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 二期需求大屏dashboard
 */

@Controller
@RequestMapping("/repairOrder/repairOrderDashBoard")
public class RepairOrderDashBoard {

    @Autowired
    private RepairOrderBiz repairOrderBiz;
    /**
     * 维保月数据统计
     */
    @Autowired
    private MaintainOrderBiz maintainOrderBiz;

    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @RequestMapping("/repairList")
    @ResponseBody
    public ResultDto getRepairList(String enterpriseId){
        Enterprise enterprise=enterpriseBiz.findModel(StringConverters.null2Long(enterpriseId));

        com.alibaba.fastjson.JSONObject jsonObject=new  com.alibaba.fastjson.JSONObject();
        jsonObject.put("enterprise",enterprise.getEnterpriseName());

        Date toDayBegin=DateTimeUtils.getBeginOfDay(new Date());//今天开始时间
        Date toDayEnd=DateTimeUtils.getEndOfDay(new Date());//今天结束时间
        QueryParam queryParam=new QueryParam("-1","-1");

        //未完成
        queryParam.getSqlMap().put("enterpriseId",enterpriseId);
        queryParam.getSqlMap().put("AUTOMATIC_REPAIR","AUTOMATIC_REPAIR");
        queryParam.getSqlMap().put("unComplete","COMPLETED");
        com.alibaba.fastjson.JSONArray todayOrders =repairOrderBiz.findJSONList1(queryParam,"toDashboardJSONObject").getJSONArray("list");
        jsonObject.put("todayOrders",todayOrders);
        //AB未完成
        queryParam.getSqlMap().put("grade","'A','B'");
        com.alibaba.fastjson.JSONArray todayABOrders =repairOrderBiz.findJSONList1(queryParam,"toDashboardJSONObject").getJSONArray("list");
        jsonObject.put("todayABOrders",todayABOrders);
        long todayNumber;
        long todayAB;
        long weekNumber;
        long weekAB;
        // --新增显示 本月维保数据
        long monthNumber;
        String monthCompletePercent;
        // --
        String todayCompletePercent;
        String todayABCompletePercent;
        String weekCompletePercent;
        String weekABCompletePercent;
        //今日报修
        queryParam.getSqlMap().remove("unComplete");
        queryParam.getSqlMap().remove("grade");
        queryParam.getSqlMap().put("createDate1",DateTimeUtils.formatDateToStringWithTime(toDayBegin));
        queryParam.getSqlMap().put("createDate2",DateTimeUtils.formatDateToStringWithTime(toDayEnd));
        todayNumber=repairOrderBiz.totalRecord(queryParam.getSqlMap());
        jsonObject.put("todayNumber",todayNumber);

        //今日AB类报修
        queryParam.getSqlMap().put("grade","'A','B'");
        todayAB=repairOrderBiz.totalRecord(queryParam.getSqlMap());
        jsonObject.put("todayAB",todayAB);

        //今日完成率
        queryParam.getSqlMap().put("completed","COMPLETED");
        //完成时间是今天的
        queryParam.getSqlMap().remove("grade");
        queryParam.getSqlMap().put("completeDate1",DateTimeUtils.formatDateToStringWithTime(toDayBegin));
        queryParam.getSqlMap().put("completeDate2",DateTimeUtils.formatDateToStringWithTime(toDayEnd));
        if(todayNumber==0){
            todayCompletePercent="0.0%";
        }else{
            todayCompletePercent=CalculateUtil.mul(CalculateUtil.div(repairOrderBiz.totalRecord(queryParam.getSqlMap()),todayNumber,3),100)+"%" ;
        }
        jsonObject.put("todayCompletePercent",todayCompletePercent);

        //今日AB类完成率
        queryParam.getSqlMap().put("grade","'A','B'");
        if(todayAB==0){
            todayABCompletePercent="0.0%";
        }else{
            todayABCompletePercent=CalculateUtil.mul(CalculateUtil.div(repairOrderBiz.totalRecord(queryParam.getSqlMap()),todayAB,3),100)+"%" ;
        }
        jsonObject.put("todayABCompletePercent",todayABCompletePercent);

        //本周报修
        Date weekBegin=DateTimeUtils.getBeginOfDay(DateTimeUtils.getFirstDateOfWeek(new Date()));//本周开始时间
        Date weekEnd=DateTimeUtils.getEndOfDay(DateTimeUtils.getLastDateOfWeek1(new Date()));//本周结束时间
        queryParam.getSqlMap().remove("completed");
        queryParam.getSqlMap().remove("grade");
        queryParam.getSqlMap().remove("completeDate1");
        queryParam.getSqlMap().remove("completeDate2");
        queryParam.getSqlMap().put("createDate1",DateTimeUtils.formatDateToStringWithTime(weekBegin));
        queryParam.getSqlMap().put("createDate2",DateTimeUtils.formatDateToStringWithTime(weekEnd));
        weekNumber=repairOrderBiz.totalRecord(queryParam.getSqlMap());
        jsonObject.put("weekNumber",weekNumber);

        //本周AB类报修
        queryParam.getSqlMap().put("grade","'A','B'");
        weekAB=repairOrderBiz.totalRecord(queryParam.getSqlMap());
        jsonObject.put("weekAB",weekAB);

        //本周完成率
        queryParam.getSqlMap().remove("grade");
        queryParam.getSqlMap().put("completeDate1",DateTimeUtils.formatDateToStringWithTime(weekBegin));
        queryParam.getSqlMap().put("completeDate2",DateTimeUtils.formatDateToStringWithTime(weekEnd));
        queryParam.getSqlMap().put("completed","COMPLETED");
        if(weekNumber==0){
            weekCompletePercent="0.0%";
        }else{
            weekCompletePercent=CalculateUtil.mul(CalculateUtil.div(repairOrderBiz.totalRecord(queryParam.getSqlMap()),weekNumber,3),100)+"%" ;
        }
        jsonObject.put("weekCompletePercent",weekCompletePercent);

        //本周AB类完成率
        queryParam.getSqlMap().put("grade","'A','B'");
        if(weekAB==0){
            weekABCompletePercent="0.0%";
        }else{
            weekABCompletePercent=CalculateUtil.mul(CalculateUtil.div(repairOrderBiz.totalRecord(queryParam.getSqlMap()),weekAB,3),100)+"%" ;
        }
        jsonObject.put("weekABCompletePercent",weekABCompletePercent);



        // ======新增需求 start======

        // 获取 维保本月维保数据
        Date monthBegin=DateTimeUtils.getBeginOfDay(DateTimeUtils.getFirstDateOfMonth(new Date()));//本月开始时间
        Date monthEnd=DateTimeUtils.getEndOfDay(DateTimeUtils.getLastDateOfMonth(new Date()));//本月结束时间
        queryParam.getSqlMap().remove("completed");
        queryParam.getSqlMap().remove("grade");
        queryParam.getSqlMap().remove("completeDate1");
        queryParam.getSqlMap().remove("completeDate2");
        queryParam.getSqlMap().put("searchDate1",DateTimeUtils.formatDateToStringWithTime(monthBegin));
        queryParam.getSqlMap().put("searchDate2",DateTimeUtils.formatDateToStringWithTime(monthEnd));
        monthNumber=maintainOrderBiz.totalRecordToDashboard(queryParam.getSqlMap());
        jsonObject.put("monthNumber",monthNumber);

        //本月完成率
        queryParam.getSqlMap().remove("grade");
        queryParam.getSqlMap().put("searchDate1",DateTimeUtils.formatDateToStringWithTime(monthBegin));// 本月开始时间
        queryParam.getSqlMap().put("searchDate2",DateTimeUtils.formatDateToStringWithTime(monthEnd));// 本月结束时间
        queryParam.getSqlMap().put("searchOrderStatus","COMPLETED"); // code = COMPLETED  枚举-表示已经完成的状态
        if(monthNumber==0){
            monthCompletePercent="0.0%";
        }else{
            Long aLong = maintainOrderBiz.totalRecord(queryParam.getSqlMap());
            System.err.println(aLong.toString());
            monthCompletePercent=CalculateUtil.mul(CalculateUtil.div(maintainOrderBiz.totalRecordToDashboard(queryParam.getSqlMap()),monthNumber,3),100)+"%" ;
        }
        // 添加入返回参数模型
        jsonObject.put("monthCompletePercent",monthCompletePercent);

        // ======新增需求 end======




        return ResultUtil.success(jsonObject);



    }
}
